import os
import signal
from typing import Any

from fastapi import APIRouter
from starlette.responses import JSONResponse


def get_custom_router(service_api: Any = None, prefix: str = '', tags: list = None, base_routers=True) -> APIRouter:
    if tags is None:
        tags = []
    router = APIRouter(prefix=prefix, tags=tags)

    @router.get("/ping")
    async def get_message():
        return await service_api.ping()

    @router.get('/get-pid/', response_class=JSONResponse)
    async def get_pid():
        if base_routers:
            return await service_api.get_pid()

    @router.get('/kill-service/', response_class=JSONResponse)
    async def kill_service():
        if base_routers:
            os.kill(service_api.get_pid(), signal.SIGTERM)
        return base_routers

    @router.get("/get-service-name/")
    async def get_service_name():
        if base_routers:
            return await service_api.get_service_name()

    return router
